package org.framework.common.api.query;

import java.io.Serializable;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Order;
import javax.persistence.criteria.Root;

public class OrderBean implements Serializable {

	/** serialVersionUID(long):. */
	private static final long serialVersionUID = 7279837458933506687L;

	// 排序字段
	private String sort;
	
	// 排序方式
	private String order;
	
	private enum OrderType{
		asc,desc,
		ASC,DESC
	}
	
	public OrderBean() {
		
	}
	
	public OrderBean(String sort, String order) {
		this.sort = sort;
		this.order = order;
	}
	
	/**
	 * 返回排序表达式.
	 * @param builder
	 * @param root
	 * @return
	 * @author chanlong(陈龙)
	 * @date 2016年1月20日 下午3:09:21
	 */
	public Order getExpresion(final CriteriaBuilder builder, final Root<?> root){
		switch(OrderType.valueOf(order)){
			case DESC:
			case desc:
				return builder.desc(root.get(sort));
			default:
				return builder.asc(root.get(sort));
		}
	}

	public String getSort() {
		return sort;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public String getOrder() {
		return order;
	}

	public void setOrder(String order) {
		this.order = order;
	}
}
